package org.jeecg.modules.meter.iot.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.constant.IotEventServiceMeterConstant;
import org.jeecg.entity.meter.Meter;
import org.jeecg.modules.meter.iot.IotPlatformFactory;
import org.jeecg.modules.meter.iot.command.CommandInvoker;
import org.jeecg.modules.meter.iot.entity.MeterBaseDto;
import org.jeecg.modules.meter.iot.entity.MeterCollectInfoDto;
import org.jeecg.modules.meter.iot.entity.MeterOnlineStatusDto;
import org.jeecg.modules.meter.iot.entity.MeterSwitchStatusDto;
import org.jeecg.modules.meter.service.MeterIotService;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Description 智能能耗 下发服务
 * @Author liurui
 * @Date 2020/12/04 17:22
 * @Version 1.0
 */
@Slf4j
@Service
public class MeterIotServiceImpl implements MeterIotService {

    //临时增加测试返回数据
    static boolean isTest = true;

    @Resource
    private IotPlatformFactory iotPlatformFactory;

    //动态注入
    @Resource
    private CommandInvoker commandInvoker;

    @Override
    public List<MeterCollectInfoDto> getCollectInfo(String meterKey, String productKey,String requestUrl, String iotType)  {
        log.info("MeterIotServiceImpl getCollectInfo request meterKey:{},requestUrl:{},iotType:{}",meterKey,requestUrl,iotType);
        //可以注入不同厂商
        commandInvoker.setIotCommand(iotPlatformFactory.createIotPlatform(iotType));
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("meterKey" , meterKey);
        try {
            JSONArray array =  commandInvoker.serviceInvokeWithArrayData(jsonObject.toJSONString(), productKey,requestUrl,IotEventServiceMeterConstant.COLLECT_INFO);
            if (!CollectionUtils.isEmpty(array)) {
                List<MeterCollectInfoDto> list = JSONObject.parseArray(array.toJSONString(), MeterCollectInfoDto.class);
                return list;
            }
        }catch (Exception e){
            log.error("MeterIotServiceImpl getCollectInfo e: {}" , e);
        }
        return null;
    }

    @Override
    public MeterBaseDto saveMeterBase(String meterKey,String meterCode, String collectorAddress,Integer meterType, String productKey, String requestUrl, String iotType) {
        log.info("MeterIotServiceImpl saveMeterBase request");
        //可以注入不同厂商
        commandInvoker.setIotCommand(iotPlatformFactory.createIotPlatform(iotType));
        JSONObject jsonObject = new JSONObject();
        if (meterKey == null){
            meterKey = "";
        }
        jsonObject.put("meterKey",meterKey);
        jsonObject.put("meterCode" , meterCode);
        jsonObject.put("collectorAddress",collectorAddress);
        jsonObject.put("meterType",meterType);
        try {
            String data =  commandInvoker.serviceInvokeWithData(jsonObject.toJSONString(), productKey,requestUrl,IotEventServiceMeterConstant.SAVE_METER_BASE);
            if (data != null){
                MeterBaseDto meterBaseDto = JSONObject.parseObject(data, MeterBaseDto.class);
                return meterBaseDto;
            }
        }catch (Exception e){
            log.error("MeterIotServiceImpl saveMeterBase e: {}" , e);
        }
        return null;
    }

    @Override
    public boolean delMeterBase(String meterKey, String productKey, String requestUrl, String iotType) {
        log.info("MeterIotServiceImpl delMeterBase request");
        //可以注入不同厂商
        commandInvoker.setIotCommand(iotPlatformFactory.createIotPlatform(iotType));
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("meterKey" , meterKey);
        try {
            return  commandInvoker.serviceInvoke(jsonObject.toJSONString(), productKey,requestUrl,IotEventServiceMeterConstant.DEL_METER_BASE);
        }catch (Exception e){
            log.error("MeterIotServiceImpl delMeterBase e: {}" , e);
        }
        return false;
    }

    @Override
    public MeterBaseDto replaceMeter(String meterKey, String newMeterCode, String collectorAddress, String productKey, String requestUrl, String iotType) {
        log.info("MeterIotServiceImpl replaceMeter request");
        //可以注入不同厂商
        commandInvoker.setIotCommand(iotPlatformFactory.createIotPlatform(iotType));
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("meterKey" , meterKey);
        jsonObject.put("newMeterCode" , newMeterCode);
        jsonObject.put("collectorAddress" , collectorAddress);
        try {
            String data =   commandInvoker.serviceInvokeWithData(jsonObject.toJSONString(), productKey,requestUrl,IotEventServiceMeterConstant.REPLACE_METER);
            if (data != null){
                MeterBaseDto meterBaseDto = JSONObject.parseObject(data, MeterBaseDto.class);
                return meterBaseDto;
            }
        }catch (Exception e){
            log.error("MeterIotServiceImpl replaceMeter e: {}" , e);
        }
        return null;
    }

    @Override
    public List<MeterSwitchStatusDto> getSwitchStatus(String meterKey, String productKey, String deviceName)  {
        log.info("MeterIotServiceImpl getSwitchStatus request");

        //可以注入不同厂商
        commandInvoker.setIotCommand(iotPlatformFactory.createIotPlatform(deviceName));
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("meterKey" , meterKey);
        try{
            JSONArray array =  commandInvoker.serviceInvokeWithArrayData(jsonObject.toJSONString() , IotEventServiceMeterConstant.SWITCH_STATUS  ,  productKey ,  deviceName);
            if (!CollectionUtils.isEmpty(array)) {
                return JSONObject.parseArray(array.toJSONString(), MeterSwitchStatusDto.class);
            }
        }catch (Exception e){
            e.printStackTrace();
            log.error("MeterIotServiceImpl getSwitchStatus e: {}" , e);
        }
        return null;
    }

    @Override
    public List<MeterOnlineStatusDto> getOnlineStatus(String meterCode, String productKey, String deviceName) {
        log.info("MeterIotServiceImpl getOnlineStatus request");

        //可以注入不同厂商
        commandInvoker.setIotCommand(iotPlatformFactory.createIotPlatform(deviceName));

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("meterCode" , meterCode);

        try{
            JSONArray array =  commandInvoker.serviceInvokeWithArrayData(jsonObject.toJSONString() , IotEventServiceMeterConstant.ONLINE_STATUS  ,  productKey ,  deviceName);
//            //增加测试
//            if (isTest){
//                return getTest3();
//            }
            if (!CollectionUtils.isEmpty(array)) {
                return JSONObject.parseArray(array.toJSONString(), MeterOnlineStatusDto.class);
            }
        }catch (Exception e){
            e.printStackTrace();
            log.error("MeterIotServiceImpl getOnlineStatus e: {}" , e);
        }
        return null;
    }

    @Override
    public boolean meterPayRequest(String meterCode, String requestCode, String orderNo, String amount, String productKey, String deviceName)  {
        log.info("MeterIotServiceImpl meterPayRequest request");

        //可以注入不同厂商
        commandInvoker.setIotCommand(iotPlatformFactory.createIotPlatform(deviceName));

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("meterCode" , meterCode);
        jsonObject.put("requestCode" , requestCode);
        jsonObject.put("orderNo" , orderNo);
        jsonObject.put("amount" , amount);

        try{
            return commandInvoker.serviceInvoke(jsonObject.toJSONString() , IotEventServiceMeterConstant.METER_PAY ,  productKey ,  deviceName);
        }catch (Exception e){
            e.printStackTrace();
            log.error("MeterIotServiceImpl meterPayRequest e: {}" , e);
        }
        return false;
    }

    @Override
    public boolean controlSwitchRequest(String meterKey, String requestCode, int state, String productKey, String requestUrl,String iotType)  {
        log.info("MeterIotServiceImpl controlSwitchRequest request");

        //可以注入不同厂商
        commandInvoker.setIotCommand(iotPlatformFactory.createIotPlatform(iotType));

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("meterKey" , meterKey);
        jsonObject.put("requestCode" , requestCode);
        jsonObject.put("state" , state);
        try {
            return commandInvoker.serviceInvoke(jsonObject.toJSONString(),productKey ,requestUrl, IotEventServiceMeterConstant.CONTROL_SWITCH);
        }catch (Exception e){
            e.printStackTrace();
            log.error("MeterIotServiceImpl controlSwitchRequest e: {}" , e);
        }
        return false;
    }

    @Override
    public boolean controlModeRequest(String meterCode, String requestCode, int mode, String productKey, String deviceName)  {
        log.info("MeterIotServiceImpl controlModeRequest request");

        //可以注入不同厂商
        commandInvoker.setIotCommand(iotPlatformFactory.createIotPlatform(deviceName));

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("meterCode" , meterCode);
        jsonObject.put("requestCode" , requestCode);
        jsonObject.put("mode" , mode);
        try{
            return commandInvoker.serviceInvoke(jsonObject.toJSONString() , IotEventServiceMeterConstant.CONTROL_MODE ,  productKey ,  deviceName);
        }catch (Exception e){
            e.printStackTrace();
            log.error("MeterIotServiceImpl controlModeRequest e: {}" , e);
        }
        return false;
    }

    @Override
    public boolean modifyPriceRequest(String meterCode, String requestCode, double price, String productKey, String deviceName)  {
        log.info("MeterIotServiceImpl modifyPriceRequest request");

        //可以注入不同厂商
        commandInvoker.setIotCommand(iotPlatformFactory.createIotPlatform(deviceName));

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("meterCode" , meterCode);
        jsonObject.put("requestCode" , requestCode);
        jsonObject.put("price" , price);
        try{
            return commandInvoker.serviceInvoke(jsonObject.toJSONString() , IotEventServiceMeterConstant.MODIFY_PRICE ,  productKey ,  deviceName);
        }catch (Exception e){
            e.printStackTrace();
            log.error("MeterIotServiceImpl controlModeRequest e: {}" , e);
        }
        return false;
    }



//    //添加测试数据-------------------------------
//    private static List<MeterCollectInfoDto> getTest1(){
//        List<MeterCollectInfoDto> list = new ArrayList<>();
//        MeterCollectInfoDto meterCollectInfoDto = new MeterCollectInfoDto();
//        meterCollectInfoDto.setMeterCode("002-S0001");
//        meterCollectInfoDto.setTotalUse("525");
//        meterCollectInfoDto.setRemainUse("203");
//        meterCollectInfoDto.setVoltage("300");
//        meterCollectInfoDto.setReadTime("2020-06-04 14:20:30");
//        meterCollectInfoDto.setOnlineStatus(0);
//        meterCollectInfoDto.setSwitchStatus(0);
//
//
//        MeterCollectInfoDto meterCollectInfoDto2 = new MeterCollectInfoDto();
//        meterCollectInfoDto2.setMeterCode("001-S0001");
//        meterCollectInfoDto2.setTotalUse("300");
//        meterCollectInfoDto2.setRemainUse("111");
//        meterCollectInfoDto2.setVoltage("245");
//        meterCollectInfoDto2.setReadTime("2020-06-04 15:20:30");
//        meterCollectInfoDto2.setOnlineStatus(1);
//        meterCollectInfoDto2.setSwitchStatus(0);
//
//        MeterCollectInfoDto meterCollectInfoDto3 = new MeterCollectInfoDto();
//        meterCollectInfoDto3.setMeterCode("001-S0008");
//        meterCollectInfoDto3.setTotalUse("700");
//        meterCollectInfoDto3.setRemainUse("222");
//        meterCollectInfoDto3.setVoltage("561");
//        meterCollectInfoDto3.setReadTime("2020-06-05 16:20:30");
//        meterCollectInfoDto3.setOnlineStatus(0);
//        meterCollectInfoDto3.setSwitchStatus(0);
//
//        MeterCollectInfoDto meterCollectInfoDto4 = new MeterCollectInfoDto();
//        meterCollectInfoDto4.setMeterCode("001-S0007");
//        meterCollectInfoDto4.setTotalUse("500");
//        meterCollectInfoDto4.setRemainUse("32");
//        meterCollectInfoDto4.setVoltage("69");
//        meterCollectInfoDto4.setReadTime("2020-06-05 11:20:30");
//        meterCollectInfoDto4.setOnlineStatus(0);
//        meterCollectInfoDto4.setSwitchStatus(1);
//
//        list.add(meterCollectInfoDto);
//        list.add(meterCollectInfoDto2);
//        list.add(meterCollectInfoDto3);
//        list.add(meterCollectInfoDto4);
//        return  list;
//    }
//
//    private static List<MeterSwitchStatusDto> getTest2(){
//        List<MeterSwitchStatusDto> list = new ArrayList<>();
//        MeterSwitchStatusDto meterSwitchStatusDto = new MeterSwitchStatusDto();
//        meterSwitchStatusDto.setMeterCode("002-S0001");
//        meterSwitchStatusDto.setSwitchStatus(0);
//
//        MeterSwitchStatusDto meterSwitchStatusDto2 = new MeterSwitchStatusDto();
//        meterSwitchStatusDto2.setMeterCode("001-S0001");
//        meterSwitchStatusDto2.setSwitchStatus(1);
//
//        MeterSwitchStatusDto meterSwitchStatusDto3 = new MeterSwitchStatusDto();
//        meterSwitchStatusDto3.setMeterCode("001-S0008");
//        meterSwitchStatusDto3.setSwitchStatus(0);
//
//        MeterSwitchStatusDto meterSwitchStatusDto4 = new MeterSwitchStatusDto();
//        meterSwitchStatusDto4.setMeterCode("001-S0007");
//        meterSwitchStatusDto4.setSwitchStatus(1);
//
//        list.add(meterSwitchStatusDto);
//        list.add(meterSwitchStatusDto2);
//        list.add(meterSwitchStatusDto3);
//        list.add(meterSwitchStatusDto4);
//
//        return list;
//    }
//
//    private static List<MeterOnlineStatusDto> getTest3() {
//        List<MeterOnlineStatusDto> list = new ArrayList<>();
//
//        MeterOnlineStatusDto meterOnlineStatusDto = new MeterOnlineStatusDto();
//        meterOnlineStatusDto.setMeterCode("002-S0001");
//        meterOnlineStatusDto.setOnlineStatus(1);
//
//        MeterOnlineStatusDto meterOnlineStatusDto2 = new MeterOnlineStatusDto();
//        meterOnlineStatusDto2.setMeterCode("001-S0001");
//        meterOnlineStatusDto2.setOnlineStatus(2);
//
//        MeterOnlineStatusDto meterOnlineStatusDto3 = new MeterOnlineStatusDto();
//        meterOnlineStatusDto3.setMeterCode("001-S0008");
//        meterOnlineStatusDto3.setOnlineStatus(1);
//
//        MeterOnlineStatusDto meterOnlineStatusDto4 = new MeterOnlineStatusDto();
//        meterOnlineStatusDto4.setMeterCode("001-S0007");
//        meterOnlineStatusDto4.setOnlineStatus(1);
//
//        list.add(meterOnlineStatusDto);
//        list.add(meterOnlineStatusDto2);
//        list.add(meterOnlineStatusDto3);
//        list.add(meterOnlineStatusDto4);
//
//        return  list ;
//    }
}
